import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
import { useUserStore } from '@/stores/user';
import { message } from 'ant-design-vue';

const routes: Array<RouteRecordRaw> = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/',
    component: () => import('@/layouts/DefaultLayout.vue'),
    meta: { requiresAuth: true },
    children: [
      {
        path: '',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index.vue')
      },
      {
        path: 'article/list',
        name: 'ArticleList',
        component: () => import('@/views/article/list.vue')
      },
      {
        path: 'article/edit',
        name: 'ArticleCreate',
        component: () => import('@/views/article/edit.vue')
      },
      {
        path: 'article/edit/:id',
        name: 'ArticleEdit',
        component: () => import('@/views/article/edit.vue')
      },
      {
        path: 'article/preview/:id',
        name: 'ArticlePreview',
        component: () => import('@/views/article/preview.vue')
      },
      {
        path: 'category/list',
        name: 'CategoryList',
        component: () => import('@/views/category/list.vue')
      },
      {
        path: 'comment/list',
        name: 'CommentList',
        component: () => import('@/views/comment/list.vue')
      }
    ]
  }
];

const router = createRouter({
  history: createWebHistory(),
  routes
});

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore();
  
  if (to.meta.requiresAuth && !userStore.isLoggedIn) {
    message.warning('请先登录');
    next({ name: 'Login', query: { redirect: to.fullPath } });
  } else {
    next();
  }
});

export default router; 